# -*- coding: utf-8 -*-
"""
API蓝图初始化文件
功能：创建和配置API蓝图，集成Swagger文档
作者：东篱馆主
创建时间：2024-12-19
修改时间：2025-01-19
"""

from flask import Blueprint
from flask_restx import Api, Resource, fields
from datetime import datetime
from config.swagger_config import SWAGGER_CONFIG, COMMON_RESPONSES

# 创建API蓝图
api_bp = Blueprint('api', __name__)

# 创建API实例并集成Swagger
api = Api(
    api_bp,
    title=SWAGGER_CONFIG['title'],
    version=SWAGGER_CONFIG['version'],
    description=SWAGGER_CONFIG['description'],
    doc='/docs/',
    validate=True,
    ordered=True
)


# 定义响应模型
health_response_model = api.model('HealthResponse', {
    'status': fields.String(description='系统状态', example='healthy'),
    'message': fields.String(description='状态消息', example='MetasoCreator API服务运行正常'),
    'timestamp': fields.String(description='时间戳', example='2025-01-19T10:00:00'),
    'version': fields.String(description='版本号', example='1.0.0')
})

# api_info_response_model 已移除，因为API信息接口已废弃


@api.route('/health')
class HealthCheck(Resource):
    """
    系统健康检查接口
    """
    
    @api.doc('health_check')
    @api.marshal_with(health_response_model)
    @api.response(200, 'Success', health_response_model)
    def get(self):
        """
        获取系统健康状态
        
        返回系统运行状态和基本信息
        """
        return {
            'status': 'healthy',
            'message': 'MetasoCreator API服务运行正常',
            'timestamp': datetime.now().isoformat(),
            'version': '1.0.0'
        }


# API根路径已废弃，所有API访问请通过Swagger文档界面进行
# Swagger文档地址: /api/docs/


# 导入其他API模块的namespace
try:
    from .search import search_ns
    api.add_namespace(search_ns, path='/search')
except ImportError as e:
    print(f"Warning: Could not import search API: {e}")

# 内容模块已移除

try:
    from .qa import qa_ns
    api.add_namespace(qa_ns, path='/chat')
except ImportError as e:
    print(f"Warning: Could not import QA API: {e}")

# 注册网页读取接口
try:
    from .reader import reader_ns
    api.add_namespace(reader_ns, path='/reader')
except ImportError as e:
    print(f"Warning: Could not import reader API: {e}")